package com.gem.Customer.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.gem.dao.CustomerDao;
import com.gem.entity.Customer;
import com.gem.util.Dbcp;

public class CustomerDaoImpl implements CustomerDao {

	JdbcTemplate template = new JdbcTemplate(Dbcp.getInstance().getDataSource());
	RowMapper<Customer> rowMapper = new BeanPropertyRowMapper<>(Customer.class);

	@Override
	public int insertCustomer(Customer customer) {

		return template.update("insert into gem_customer values(?,?,?)", customer.getId(), customer.getName(),
				customer.getAddress());
		/*	Connection conn = null;
				PreparedStatement pst = null;
				ResultSet rs = null;
				int result = -1;
				try {
					conn = Dbcp.getInstance().getConnection();
					pst = conn.prepareStatement("insert into gem_customer(id,name,address) values(?,?,?)");
					pst.setLong(1, customer.getId());
					pst.setString(2, customer.getName());
					pst.setString(3, customer.getAddress());
					result = pst.executeUpdate();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					Dbcp.getInstance().close(conn, pst, rs);
			}*/
	}

	@Override
	public int deleteCustomer(Long id) {
		return template.update("delete from gem_customer where id=?", id);
		//return JDBCTemplate.update(conn -> conn.prepareStatement("delete from gem_customer where id=" + id));
	}

	@Override
	public Customer selectCustomerById(Long id) {
		return template.queryForObject("select * from gem_customer where id=?", rowMapper, id);
		/*		Connection conn = null;
				PreparedStatement pst = null;
				ResultSet rs = null;
				Customer customer = null;
				try {
					conn = JdbcUtil.getConnection();
					pst = conn.prepareStatement("select * from gem_customer where id=?");
					pst.setLong(1, id);
					rs = pst.executeQuery();
					if (rs.next()) {
						String name = rs.getString("name");
						String address = rs.getString("address");
						customer = new Customer(id, name, address);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					JdbcUtil.close(conn, pst);
				}
				return customer;*/
	}

	@Override
	public Customer selectCustomerByName(String name) {
		return template.queryForObject("select * from gem_customer where name=?", rowMapper, name);
		/*	Connection conn = null;
					PreparedStatement pst = null;
					ResultSet rs = null;
					Customer customer = null;
					try {
						conn = JdbcUtil.getConnection();
						pst = conn.prepareStatement("select * from gem_customer where name=?");
						pst.setString(1, name);
						rs = pst.executeQuery();
						if (rs.next()) {
							Long id = rs.getLong("id");
							String address = rs.getString("address");
							customer = new Customer(id, name, address);
						}
					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						JdbcUtil.close(conn, pst);
					}
		return customer;*/
	}

	@Override
	public Customer selectCustomerByAddress(String address) {
		return template.queryForObject("select * from gem_customer where address=?", rowMapper, address);
		/*	Connection conn = null;
			PreparedStatement pst = null;
			ResultSet rs = null;
			Customer customer = null;
			try {
				conn = JdbcUtil.getConnection();
				pst = conn.prepareStatement("select * from gem_customer where address=?");
				pst.setString(1, address);
				rs = pst.executeQuery();
				if (rs.next()) {
					Long id = rs.getLong("id");
					String name = rs.getString("name");
					customer = new Customer(id, name, address);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				JdbcUtil.close(conn, pst);
			}
			return customer;*/
	}

	@Override
	public int updateCustomer(Customer customer) {
		return template.update("update gem_customer set id=?,name=?,address=? where id=?", customer.getId(),
				customer.getName(), customer.getAddress(), customer.getId());
		/*Connection conn = null;
		PreparedStatement pst = null;
		int result = 0;
		try {
			conn = JdbcUtil.getConnection();
			Long id = customer.getId();
			String name = customer.getName();
			String address = customer.getAddress();
			String sql = "update gem_customer set id=" + id;
			if (id != null) {
				if (name != null) {
					sql += ",name='" + name + "'";
				}
				if (address != null) {
					sql += ",address='" + address + "'";
				}
			}
			sql += "where id =" + id;
			pst = conn.prepareStatement(sql);
			result = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(conn, pst);
		}
		return result;*/

	}

	@Override
	public List<Customer> selectAllCustomer() {
		return template.query("select * from gem_customer", rowMapper);
	}
	/*Customer customer = null;
	List<Customer> customers = new ArrayList<Customer>();
	Connection conn = null;
	PreparedStatement pst = null;
	ResultSet rs = null;
	try {
		conn = JdbcUtil.getConnection();
		pst = conn.prepareStatement("select * from gem_customer");
		rs = pst.executeQuery();
		while (rs.next()) {
			customer = new Customer(rs.getLong("id"), rs.getString("name"), rs.getString("address"));
			customers.add(customer);
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		JdbcUtil.close(conn, pst, rs);
	}
	return customers;
	
	}
	}*/

}
